//
//  CZMeiTuanRefreshView.swift
//  新浪微博
//
//  Created by 名品导购网MPLife.com on 2017/12/8.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit

class CZMeiTuanRefreshView: CZRefreshView {
    
    private lazy var buildImage:UIImageView = UIImageView()
    private lazy var earthIcon:UIImageView = UIImageView()
    private lazy var kangraroouIcon:UIImageView = UIImageView()
    var parentViewHeight:CGFloat = 0{
        didSet{
            //从30到126
            if parentViewHeight < 30 { //scale 0.2
                return
            }
            //高度差
            //23-->1
            //126 == 0 -->1
            var scale:CGFloat
            if parentViewHeight>126 {
                scale = 1
            }else{
                scale = 1-((126 - parentViewHeight)/(126-30))
            }
            kangraroouIcon.transform = CGAffineTransform(scaleX: scale, y: scale)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpTheUI()
        let bgImage1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let bgImage2 = #imageLiteral(resourceName: "icon_building_loading_2")//竟然可以这些写图片
        buildImage.image = UIImage.animatedImage(with: [bgImage1,bgImage2], duration: 0.5)
        //地球
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = -2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 3
        anim.isRemovedOnCompletion = false
        earthIcon.layer.add(anim, forKey: nil)
        //袋鼠
        //设置锚点 定位点
        let kImage1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let kImage2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")//竟然可以这些写图片
        kangraroouIcon.image = UIImage.animatedImage(with: [kImage1,kImage2], duration: 0.5)

        kangraroouIcon.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        //设置center
        let x = UIScreen.main.bounds.width * 0.5
        let y = CGFloat(300 - 30)
        kangraroouIcon.center = CGPoint(x: x, y: y)
        kangraroouIcon.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}

extension CZMeiTuanRefreshView{
    func setUpTheUI() {
        buildImage.frame = CGRect(x: (UIScreen.main.bounds.width-200)/2, y: 300-150, width: 200, height: 150)
        buildImage.image = UIImage(named: "icon_building_loading_1")
        self.addSubview(buildImage)
        
        earthIcon.frame = CGRect(x: buildImage.frame.origin.x, y: buildImage.frame.origin.y + buildImage.frame.size.height-30, width: 180, height: 180)
        earthIcon.center.x = buildImage.center.x
        earthIcon.image = UIImage(named: "icon_earth")
        self.addSubview(earthIcon)
        
        kangraroouIcon.frame = CGRect(x: (earthIcon.frame.size.width-60)/2, y:earthIcon.frame.origin.y-60, width: 60, height: 60)
        kangraroouIcon.image = UIImage(named: "icon_small_kangaroo_loading_1")
        kangraroouIcon.center.x = earthIcon.center.x
        self.addSubview(kangraroouIcon)
    }
}
